﻿using System;
using System.Collections.Generic;

using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module.Core.BusinessLogic;
using Module.Core.Common;
using Module.Core.EntitiesInfo;

namespace Web.Cms.Member
{
    public partial class Register : System.Web.UI.Page
    {
        #region Khai báo
        private int idMember = 0;
       
       
        #endregion

        #region PageLoad
        protected void Page_Load(object sender, EventArgs e)
        {
            idMember = ObjConvertUtility.ToInt32(Request.QueryString["idMember"]);
            
            try
            {               
                if (!Page.IsPostBack)
                {                   
                    LoadData();
                }
            }
            catch (Exception ex)
            {
            }


        }
        #endregion

        #region LoadData
        private void LoadData()
        {
            try
            {
                if (idMember > 0)
                {
                    buttonUpdate.Visible = true;
                    buttonAdd.Visible = false;
                    MemberInfo info = MemberBL.Select(idMember.ToString(),2);
                    if (info != null)
                    {
                        txtFullName.Text = info.FullName;
                        txtAddress.Text = info.Address;
                        txtEmail.Text = info.Email;
                        txtDateBirth.SelectedDate = info.DateBirth;
                        txtPass.Text = info.Pass;
                        txtPhoneNumber.Text = info.PhoneNumber;
                        txtCodeMemberParent.Text = info.CodeMemberParent;
                        txtCodeMemberParent.Enabled = false;
                        lblPass.Visible = false;
                        lblConfirmPassword.Visible = false;
                        txtConfirmPassword.Visible = false;
                        txtPass.Visible = false;
                        txtPriceMount.Text = info.PriceMount.ToString();
                        txtNumberAcount.Text = info.NumberAcount;
                        txtNameBank.Text = info.NameBank;
                        txtCMTND.Text = info.CMTND;
                    }
                }
                else
                {
                    buttonUpdate.Visible = false;
                    buttonAdd.Visible = true;
                 
                }
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }
        #endregion
       
        #region FillData
        void FillData(MemberInfo info)
        {
            info.IdMember = idMember;
            info.FullName = txtFullName.Text;
            info.Address = txtAddress.Text;
            info.Email = txtEmail.Text; 
            info.Status = Const.show;
            info.DateBirth = txtDateBirth.SelectedDate;
            info.Pass = MyUtility.MySecurity.Encrypt_MD5(txtPass.Text);
            info.PhoneNumber = txtPhoneNumber.Text;
            info.CodeMemberParent =txtCodeMemberParent.Text;
            info.CreatedOn =DateTime.Now;          
            info.PriceMount =int.Parse(txtPriceMount.Text);
            info.Checktype = 0;          
            info.CodeMember = MyUtility.MySecurity.GenerateRandomCode();
            info.UserName = "";
            info.IsActive = false;
            info.NumberAcount = txtNumberAcount.Text;
            info.NameBank = txtNameBank.Text;
            info.CMTND = txtCMTND.Text;
            info.Promotional = 1;

        }
        #endregion
                    
        #region thêm mới
        protected void buttonAdd_Click(object sender, EventArgs e)
        {
            try
            {
                MemberInfo info = new MemberInfo();
                FillData(info);
                int idMember = MemberBL.Insert(info);
                if (idMember > 0)
                {                   
                    lblUpdateStatus.Text = "Bạn đã thêm mới thành công";
                    ClearText();                   
                }
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }

        #endregion

        #region Sửa
        protected void buttonUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                MemberInfo info = new MemberInfo();
                FillData(info);
                MemberBL.Update(info);
                lblUpdateStatus.Text = "Bạn đã chỉnh sửa thành công";
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }
        #endregion

        #region ClearText
        void ClearText()
        {
            txtEmail.Text = "";
            txtAddress.Text = "";
            txtPass.Text = "";
            txtPhoneNumber.Text = "";
           txtCodeMemberParent.Text = "";
        }

        #endregion

        #region  mã hóa mật khẩu md5
        public static string CreatePasswordHash(string pwd)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(pwd, "MD5");
        }
        public static string password(string cpass)
        {
            byte[] pass = Encoding.UTF8.GetBytes(cpass);
            MD5 md5 = new MD5CryptoServiceProvider();
            string strPassword = Encoding.UTF8.GetString(md5.ComputeHash(pass));
            return strPassword;
        }
        #endregion

    }
}